/**
 *
 * 一个用于控制相机移动的缓动类CameraMove.js
 *
 * Daniel
 * 2018.03.05
 *
 * */

define(function(){
    var CameraMove = function(){
        this.camera = {};
        this.world = {};
        this.control = {};
        
        
    };
    
    
    //初始化
    CameraMove.prototype.init = function(world, cameraControl){
        this.world = world;
        this.camera = world.camera;
        this.control = cameraControl;
    };
    
    
    //相机位移
    CameraMove.prototype.cameraMoving = function() {
        var scope = this;
        return function(pointTarget){
            if(pointTarget == undefined) return;
            var psitions = new THREE.Vector3();
            var boxCC = new THREE.Box3().setFromObject(pointTarget);
            var boxCenter = new THREE.BoxHelper(pointTarget).geometry.boundingSphere.center;
            var geometryCenter = boxCenter.clone();
            var cameraTran = scope.control;
            var target = cameraTran.target;
            var newLookAt = new THREE.Vector3().copy(geometryCenter);
            boxCC.target = geometryCenter;
            target.copy(newLookAt);
             psitions.copy(scope.cameraFocussing(scope.camera,boxCC).clone());            //根据定比分点求得
           // psitions.copy(scope.cameraCompute(scope.camera,boxCC).clone());           //根据法向量求得
            scope.aniamteCamera(scope.camera.position,psitions,500);
            cameraTran.update();
            cameraTran.saveState();
        };
        
    };
    
    
    //缓动动画
    CameraMove.prototype.aniamteCamera = function (objectP,targetV,time,callback){
        var anime = createjs.Tween;
        var target = objectP;
        var targetPop = targetV.clone();
        var Animate = createjs.Ease.linear;
        var back = callback || function(){};
        anime.get(target).to(targetPop, time, Animate).call(back);
    };
    
    
    //相机对焦
    CameraMove.prototype.cameraFocussing = function(cameraObj,center){
        var initPoint = cameraObj.position;
        var tanA = (cameraObj.fov-10)/2;
        var startC = center.max.clone();
        var endC = center.min.clone();
        var geoC = center.target;
    
        var targetSide = startC.distanceTo(endC);            //目标距离d
        var lengthDis =  initPoint.distanceTo(geoC);          //距离总长
        var targetHigth = targetSide / ((Math.tan(tanA/180*Math.PI))*2);       //中断值到中心点的距离
        var lengthTarget = lengthDis - targetHigth;              //相机到目标位置的距离
        var l =  lengthTarget/targetHigth;
        if(l<-1) l = -0.9;
        
        //直线方程
        var toPop = new THREE.Vector3();
        var popX, popY, popZ,x1,y1,z1,x2,y2,z2;           //中断值
        x1 = initPoint.x;
        y1 = initPoint.y;
        z1 = initPoint.z;
    
        x2 = geoC.x;
        y2 = geoC.y;
        z2 = geoC.z;
    
        popX = Math.floor((x1 + l*x2)/(1+l))*100/100;
        popY = Math.floor((y1 + l*y2)/(1+l))*100/100;
        popZ = Math.floor((z1 + l*z2)/(1+l))*100/100;
    
        toPop.set(popX,popY,popZ);
        return toPop;
    };
    
    CameraMove.prototype.cameraCompute = function(cameraObj,center){
        var toPop = new THREE.Vector3();
        var initPoint = cameraObj.position;
        var startC = center.max.clone();
        var endC = center.min.clone();
        var geoC = center.target;
        var vecotr = initPoint.clone().sub(new THREE.Vector3()).divideScalar(initPoint.length()).clone();
    
        var targetSide = startC.distanceTo(endC)/2;            //目标距离d
            vecotr.multiplyScalar(targetSide).add(geoC);
    
        toPop.set(vecotr.x,vecotr.y,vecotr.z);
        return toPop.clone();
    };
    
    
    
    return CameraMove;
    
    
    
});